﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ComponentAce.Compression.Libs.zlib;
using System.Net;
using com.lover.astd.common.manager;
using com.lover.common;

namespace com.lover.astd.game.ui
{
    public partial class Translator : Form
    {
        public Translator()
        {
            InitializeComponent();
        }

        private void btn_open_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.CheckFileExists = true;
            DialogResult result = dlg.ShowDialog();
            if (result != DialogResult.OK) return;

            FileStream instream = new FileStream(dlg.FileName, FileMode.Open);

            string retstr = "";
            MemoryStream outst = new MemoryStream();
            ZOutputStream zoutstream = new ZOutputStream(outst);
            try
            {
                TransferMgr.CopyStream(instream, zoutstream);
                outst.Position = 0;
                StreamReader reader = new StreamReader(outst);
                retstr = reader.ReadToEnd();
                // remove the first 3 utf-8 BOM char (EF BB BF)
                retstr = retstr.Substring(retstr.IndexOf("<results>"));

                txt_result.Text = retstr;
            }
            catch(Exception ex){
                UiUtils.getInstance().error("出错了, " + ex.Message);
            }
            finally
            {
                instream.Close();
                outst.Close();
                zoutstream.Close();
            }
        }

        private void btn_openUrl_Click(object sender, EventArgs e)
        {
            if (txt_url.Text == "")
            {
                UiUtils.getInstance().info("还未输入网址");
                return;
            }
            CookieContainer cook_container = new CookieContainer(10000, 1000, 409600);
            if (web_view != null && web_view.Document != null)
            {
                string cookies = web_view.Document.Cookie;
                if (cookies != null)
                {
                    cookies = cookies.Replace(';', ',');
                    cook_container.SetCookies(new Uri(txt_url.Text), cookies);
                }
            }
            string url = txt_url.Text;
            List<Cookie> game_cookies = new List<Cookie>();
            CookieCollection cooks = cook_container.GetCookies(new Uri(url));
            foreach (Cookie cc in cooks) game_cookies.Add(cc);
            string result = TransferMgr.doGet(url, ref game_cookies);
            txt_result.Text = result;
        }
    }
}
